//
//  CYRechargeRecordViewController.m
//  cyjf
//
//  Created by tauCross on 2017/5/17.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYRechargeRecordViewController.h"

#import "CYRechargeRecordCell.h"



@interface CYRechargeRecordViewController ()

@property(nonatomic, assign)NSInteger page_no;
@property(nonatomic, assign)BOOL loading;
@property(nonatomic, assign)BOOL hasMore;
@property(nonatomic, assign)BOOL errorShowed;

@end


@implementation CYRechargeRecordViewController

- (void)loadUI
{
    self.navigationItem.title = @"充值记录";
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
}

- (void)loadData
{
    self.page_no = 1;
    self.loading = NO;
    self.hasMore = YES;
    self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:@[] delegate:(id)[NICellFactory class]];
    self.tableView.dataSource = self.tableModel;
    [self.tableView reloadData];

    [self loadDataFromServer];
}

- (void)loadDataFromServer
{
    if(self.loading)
    {
        return;
    }
    if(self.hasMore == NO)
    {
        return;
    }
    self.loading = YES;
    @weakify(self);
    [CYStandardDataAPI user_rechargeRecordWithPage_no:@(self.page_no) complete:^(CYResponse *response) {
        @strongify(self);
        if(response.code == 0)
        {
            NSMutableArray *cells = [NSMutableArray array];
            if([response.content isKindOfClass:[NSArray class]])
            {
                self.hasMore = [response.content count] >= CY_API_PAGE_SIZE;
                self.page_no ++;
                for(NSDictionary *record in response.content)
                {
                    if([record isKindOfClass:[NSDictionary class]])
                    {
                        CYRechargeRecordCellObject *cell = [CYRechargeRecordCellObject objectWithCellClass:[CYRechargeRecordCell class]];
                        cell.model = [CYDataAPIRechargeRecordModel mj_objectWithKeyValues:record];
                        [cells addObject:cell];
                    }
                }
            }
            [self.tableModel addObjectsFromArray:cells];
            [self.tableView reloadData];
            self.loading = NO;
        }
        else
        {
            if(self.errorShowed == NO)
            {
                [SVProgressHUD showErrorWithStatus:response.message];
                self.errorShowed = YES;
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                @strongify(self);
                self.loading = NO;
            });
        }
    }];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.tableView)
    {
        if(scrollView.contentSize.height - scrollView.contentOffset.y < scrollView.height * 2)
        {
            [self loadDataFromServer];
        }
    }
}

@end
